<template>
  <div class="flex items-center justify-between">
    <h2 class="text-xl font-bold mb-4">作业记录</h2>
  </div>
  <el-table :data="assignments" style="width: 100%">
    <el-table-column prop="des" label="作业内容" />
    <el-table-column prop="createdTime" label="作业发布时间" />
    <el-table-column prop="studentHomeworkFiles" label="文件">
      <template #default="{ row }">
        <el-button type="primary" @click="handleView(row)">点击查看</el-button>
      </template>
    </el-table-column>
    <el-table-column prop="reviserName" label="批改人"/>
      <el-table-column prop="grades" label="成绩"/>
      <el-table-column prop="comments" label="评论"/>
  </el-table>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import type { Course } from "../../teacher/composables/useProject";
import { request } from "../../../utils/request";
import {
  getHomeworkByCourseHomeworkId,
  getHomeworkListByCourseId,
} from "../../../api/modules/course";
import { useUserStore } from "../../../store/user";
import { ElMessage } from "element-plus";

const props = defineProps<{
  data: Course[];
}>();

function isSubmit(item: Course) {
  return item.files.length;
}

const userStore = useUserStore();
const userInfo = computed(() => userStore.userInfo.value);

const assignments = ref([]);

function getSign() {
  request({
    fn: getHomeworkByCourseHomeworkId,
    params: ["", userInfo.value.id],
    success(res) {
      const data = res.data || [];
      data.forEach((item) => {
        if (item.studentId === userInfo.value.id) {
          assignments.value.push(item);
        }
      });
    },
  });
}

getSign();

function handleView(item: any) {
  const fileurl = item?.studentHomeworkFiles?.[0].fileUrl;
  if (!fileurl) {
    ElMessage.error("文件不存在");
    return;
  }
  window.open(`/preview?url=${fileurl}`);
}
</script>
<style scoped lang="scss"></style>
